
%{
AUTHOR: Felipe Arteaga
-------------------------------------------------------------------------
PROJECT: Warnings
-------------------------------------------------------------------------
DESCRIPTION:
Plot histogram of VA, SIMCE, teacher spending, student spending

Congested vs Uncongested
=========================================================================
%}


clearvars -except projectDir projectDirData fromMainWarningsPaper
clc;close all;fclose('all');feature('DefaultCharacterSet','UTF-8');

if(not(exist('projectDir','var')==1&&exist('projectDirData','var')==1&&exist('fromMainWarningsPaper','var')==1&&fromMainWarningsPaper))
    pcName=char(java.lang.System.getProperty('user.name'));
    if(strcmp(pcName,'felipe'))
        % PC Felipe
        myDir='/Users/felipe/Dropbox/';
        projectDir=[myDir,'git/warnings/'];
        projectDirData=[myDir,'projects/warnings/'];
        addpath(genpath([myDir,'/myMatlabFunctions/']));
    end
end
compileLatexTable=false;

%%
dirDataAux=[projectDirData,'/data/chile/auxiliar'];
dirPlots=[projectDir,'/paper/figuresCL/other/'];

anho=2020;
anhoStr=sprintf('%i',anho);

dataOferta=readtable(sprintf('%s/oferta_1_%i.csv',dirDataAux,anho),'delimiter',',');

 
schoolsXs=readtable([dirDataAux,'/schoolsXs',anhoStr,'.csv'],'delimiter',',');
dataOferta=outerjoin(dataOferta,schoolsXs,'keys',{'rbd'},'mergeKeys',true,'type','left');



vaAll=readtable([dirDataAux,'/FirmData.csv']);
vaAll=vaAll(vaAll.Year==2016,:);
vaAll.rbd=vaAll.School_RBD;
vaAll.valueAdded=vaAll.VA2_AVE ; % Este es el con mejores controles según Chris.
vaAll.PerTeacherSpending=vaAll.PerTeacherSpending/1000;
vaAll.PerCapSpending=vaAll.PerCapSpending/1000;

dataLlenos=readtable(sprintf('%s/llenos%s.csv',dirDataAux,anhoStr));
simces=readtable([dirDataAux,'/simcePerGradeParchado2018.csv'],'delimiter',',');
simces.testScore_mat=simces.matParchado;
dataOferta=outerjoin(dataOferta,vaAll,'keys',{'rbd'},'mergeKeys',true,'type','left');
dataOferta=outerjoin(dataOferta,dataLlenos,'keys',{'rbd','cod_curso'},'mergeKeys',true,'type','left','rightVariables',{'lleno','conLista'});
dataOferta=outerjoin(dataOferta,simces,'keys',{'rbd','cod_nivel'},'mergeKeys',true,'type','left');

dataOferta.cong=dataOferta.conLista==1;

dataOferta.sirve=dataOferta.newMarket==13001&dataOferta.cod_nivel==-1;
dataOferta.fee=dataOferta.fee/750;
%%
close all

%vars={'valueAdded','PerTeacherSpending','PerCapSpending','Principal_Score_Math','TeacherMath_WeightedAve','testScore_mat'};
vars={'valueAdded','PerTeacherSpending','PerCapSpending','fee','meanSep','sizePerNivel'};
varL={'Value added','Per teacher spending (1000USD)','Per student spending (1000USD)','School monthly fee (USD)','Share of vulnerable students','Total enrollment per grade'};
for v=1:length(vars)

figure
    compareHistograms({dataOferta.(vars{v})(dataOferta.cong&dataOferta.sirve),dataOferta.(vars{v})(not(dataOferta.cong)&dataOferta.sirve)},...
        'labels',{'Oversubscribed','Undersubscribed'},'ignoreMissing',true)
    xlabel(varL{v})
    %title(sprintf('Histogram of %s - Only PKs of Urban Santiago',vars{v}))
    easyExport([dirPlots,'histCongested_',vars{v}],'width',500)

end
